package com.jzo2o.trade.service.impl;

import cn.hutool.json.JSONUtil;
import com.jzo2o.api.trade.dto.response.ExecutionResultResDTO;
import com.jzo2o.trade.config.WxPayConfig;
import com.jzo2o.trade.config.WxPayProperties;
import com.jzo2o.trade.model.domain.RefundRecord;
import com.jzo2o.trade.service.IWxRefundService;
import com.wechat.pay.java.service.refund.RefundService;
import com.wechat.pay.java.service.refund.model.AmountReq;
import com.wechat.pay.java.service.refund.model.CreateRequest;
import com.wechat.pay.java.service.refund.model.Refund;
import com.wechat.pay.java.service.refund.model.Status;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;

/**
 * \* User: chy
 * \* Date: 2025/10/9  10:02
 * \* Description:
 * \
 */
@Service
@Slf4j
public class WxRefundServiceImpl implements IWxRefundService {

    @Autowired
    private WxPayProperties wxPayProperties;
    @Autowired
    private WxPayConfig wxPayConfig;
    /**
     * 微信退款
     *
     * @param refundRecord
     * @return
     */
    @Override
    public ExecutionResultResDTO refund(RefundRecord refundRecord) {
        log.info("微信退款服务开始");
        // 创建微信退款记录
        Refund result =  wxRefundRecord(refundRecord);
        if(result == null){
            log.error("微信退款异常:{}", "微信退款失败");
            return null;
        }
        // 封装返回结果
        ExecutionResultResDTO execution = new ExecutionResultResDTO();
        execution.setRefundId(result.getRefundId());
        execution.setRefundStatus(result.getStatus().hashCode());

        execution.setRefundNo(refundRecord.getRefundNo());
        return execution;
    }
    /**
     * 创建微信退款记录
     *
     * @param refundRecord
     */
    private Refund wxRefundRecord(RefundRecord refundRecord) {

        try {
            // 创建退款服务实例
            RefundService refundService = new RefundService.Builder().config(wxPayConfig.config()).build();

            // 创建退款请求对象
            CreateRequest createRequest = new CreateRequest();

            // 设置退款参数
            createRequest.setOutTradeNo(refundRecord.getTradingOrderNo());  // 商户订单号
            createRequest.setOutRefundNo(refundRecord.getRefundNo()+  "");     // 商户退款单号
            AmountReq amountReq = new AmountReq();
            amountReq.setTotal(refundRecord.getTotal().multiply(BigDecimal.valueOf(100)).longValue());       // 原订单总金额（分）
            //amountReq.setRefund(Long.parseLong(refundRecord.getRefundAmount().multiply(BigDecimal.valueOf(100))+ ""));       // 退款金额（分）
            amountReq.setRefund(refundRecord.getRefundAmount().multiply(BigDecimal.valueOf(100)).longValue());
            amountReq.setCurrency("CNY");  // 货币类型
            createRequest.setAmount(amountReq);
            createRequest.setReason("取消订单退款");  // 退款原因
            createRequest.setNotifyUrl(wxPayProperties.getNotifyUrl());  // 异步通知地址

            // 发起退款请求
            Refund refund = refundService.create(createRequest);

            return refund;
        } catch (NumberFormatException e) {
            log.error("微信退款异常:{}", e.getMessage());
            return null;
        }
    }
}
